<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
// use Laravel\Passport\Passport;
use Illuminate\Contracts\Routing\Registrar as Router;
use Bouncer;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Router $router)
    {
        Bouncer::useAbilityModel(\App\Models\Ability::class);
        Bouncer::useRoleModel(\App\Models\Role::class);
        // Bouncer::useRoleModel(\App\Models\Role::class);
        // Passport::routes();
//        Passport::tokensExpireIn(now()->addDays(15));
//        Passport::refreshTokensExpireIn(now()->addDays(30));
//        Passport::routes(function ($passportRouter)use($router) {
//            $defaultOptions = [
//               // 'prefix' => 'oauth',
//                'namespace' => '\Laravel\Passport\Http\Controllers',
//            ];
//            $router->get('/authorize', array_merge([
//                'uses' => 'AuthorizationController@authorize',
//                'as' => 'passport.authorizations.authorize',
//            ], $defaultOptions));
//            
//            $router->group(array_merge(['middleware' => ['web', 'auth']], $defaultOptions), function ($router) {
//
//                $router->post('/authorize', [
//                    'uses' => 'ApproveAuthorizationController@approve',
//                    'as' => 'passport.authorizations.approve',
//                ]);
//
//                $router->delete('/authorize', [
//                    'uses' => 'DenyAuthorizationController@deny',
//                    'as' => 'passport.authorizations.deny',
//                ]);
//            });
//            $passportRouter->forAuthorization();
//            $passportRouter->forAccessTokens();
       // });
        if(!config('site.enableDbLog')){
            return;
        }
                \DB::listen(
            function ($sql) {
                foreach ($sql->bindings as $i => $binding) {
                    if ($binding instanceof \DateTime) {
                        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
                    } else {
                        if (is_string($binding)) {
                            $sql->bindings[$i] = "'$binding'";
                        }elseif(is_bool($binding)) {
                            $sql->bindings[$i] = $binding + 0;
                        }
                    }
                }

                // Insert bindings into query
                $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);

                $query = vsprintf($query, $sql->bindings);

                // Save the query to file
                $logFile = fopen(
                    storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
                    'a+'
                );
                fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
                fclose($logFile);
            }
        );
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        
    }
}
